package com.crazy.bookstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crazy.bookstore.entity.Book;
import com.crazy.bookstore.entity.Evaluation;
import com.crazy.bookstore.entity.MemberReadState;
import com.crazy.bookstore.mapper.BookMapper;
import com.crazy.bookstore.mapper.EvaluationMapper;
import com.crazy.bookstore.mapper.MemberMapper;
import com.crazy.bookstore.mapper.MemberReadStateMapper;
import com.crazy.bookstore.service.BookService;
import com.crazy.bookstore.vo.BookForAdminVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * @author: 卢辉宇
 * @date: 2022/12/27 20:47
 * @description:
 */
@Service
public class BookServiceImpl implements BookService {
    @Resource
    BookMapper bookMapper;

    @Resource
    EvaluationMapper evaluationMapper;

    @Resource
    MemberReadStateMapper memberReadStateMapper;

    @Override
    public IPage<Book> selectPage(Long categoryId, String order, Integer page, Integer rows) {
        /*新建一个mybatisPlus分页对象*/
        IPage<Book> iPage = new Page<>(page, rows);

        /*新建一个mybatisPlus查询条件对象*/
        QueryWrapper<Book> wrapper = new QueryWrapper<>();

        if (categoryId!=null&&categoryId!=0){
            //传入的目录分类不为空且不等于0，等于0代表查询全部
            //相当于sql语句中的where "category_id"=categoryId
            wrapper.eq("category_id", categoryId);
        }
        if(order != null){
            if(order.equals("quantity")){
                //相当于sql语句中的order by evaluation_quantity
                wrapper.orderByDesc("evaluation_quantity");
            }else if(order.equals("score")){
                wrapper.orderByDesc("evaluation_score");
            }
        }else{
            //默认排序
            wrapper.orderByDesc("evaluation_quantity");
        }

        /*调用mybatisPlus的分页查询语句*/
        iPage = bookMapper.selectPage(iPage, wrapper);
        return iPage;
    }

    /**
     * 直接调用mybatisPlus按主键查找
     * @param bookId
     * @return
     */
    @Override
    public Book selectById(Long bookId) {
        Book book = bookMapper.selectById(bookId);
        return book;
    }

    /**
     * 更新总分和人数
     * 开启事务
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateScore(){
        bookMapper.updateScore();
    }


    /**
     * 分页查询图书信息
     * @param page
     * @param row
     * @return
     */
    @Override
    public IPage<BookForAdminVO> selectBookList(Integer page, Integer row) {
        IPage<BookForAdminVO> iPage = new Page<>(page,row);
        //调用mapper的分页查询
        IPage<BookForAdminVO> bookList = bookMapper.selectBookList(iPage);
        return bookList;
    }

    /**
     * 创建图书
     * @param book
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBook(Book book) {
        bookMapper.insert(book);
    }

    /**
     * 更新图书
     * @param book
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBook(Book book) {
        bookMapper.updateById(book);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBook(Integer bookId) {
        //删除book
        bookMapper.deleteById(bookId);

        //删除短评
        QueryWrapper<Evaluation> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("book_id",bookId);
        evaluationMapper.delete(wrapper1);

        //删除会员阅读状态
        QueryWrapper<MemberReadState> wrapper2 = new QueryWrapper<>();
        memberReadStateMapper.delete(wrapper2);
    }
}
